﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Web;
using System.IO;

namespace OgilvyOne.Borad.BLL
{
    public class MailService
    {
        //sendMail(收件人邮箱 邮件内容 邮件主题)
        public static bool sendMail(string toMail, string emailBody)
        {
            string strSendEmail = "newbeetle@vw.com.cn";
            string strUserName = "大众进口汽车甲壳虫";
            string strPassWord = "newbeetlemail";
            string strEmailServerHost = "mail.vw.com.cn";
            string srtSsubject = "全新一代甲壳虫到手，德国原厂限量礼包入手！";
            StreamReader sr = null;
            try
            {
                MailMessage dim = new MailMessage();
                //new MailAddress("发件人邮箱", "发件人名称");
                dim.From = new MailAddress(strSendEmail, strUserName);
                dim.To.Add(toMail);
                dim.Subject = srtSsubject;
                dim.Body = emailBody;
                dim.IsBodyHtml = true;
                dim.BodyEncoding = System.Text.Encoding.UTF8;
                dim.Priority = System.Net.Mail.MailPriority.Normal;
                SmtpClient smtpClient = new SmtpClient();
                //发件人密码
                smtpClient.Credentials = new System.Net.NetworkCredential(dim.From.Address, strPassWord);
                smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                //邮件发送服务器
                smtpClient.Host = strEmailServerHost;
                smtpClient.Send(dim);
            }
            catch (Exception ex)
            {
                throw;
                return false;
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                    sr.Dispose();
                }
            }
            return true;
        }
        public static string generateEmail(string img,string fileName,string name, string sex, string mobile, string email, string dealer, string budget, string mention, string capcity, string carModel, string carColor, string carSeat)
        {
            StreamReader sr = new StreamReader(getPath(string.Format("{0}.html", fileName)));
            string str = null;
            try
            {
                str = sr.ReadToEnd();
                str = str.Replace("$Name$", name);
                str = str.Replace("$Sex$", sex);
                str = str.Replace("$Mobile$", mobile);
                str = str.Replace("$Email$", email);
                str = str.Replace("$Dealer$", dealer);
                str = str.Replace("$Budget$", budget);
                str = str.Replace("$Mention$", mention);
                str = str.Replace("$Capcity$", capcity);
                str = str.Replace("$CarModel$", carModel);
                str = str.Replace("$CarColor$", carColor);
                str = str.Replace("$CarSeat$", carSeat);
                str = str.Replace("$ImgName$", img);
            }
            catch (Exception e)
            {
                e.ToString();
            }
            finally
            {
                sr.Close();
            }
            return str;
        }
        private static string getPath(string path)
        {
            return HttpContext.Current.Server.MapPath(path);
        }
    }

}
